<?php
// +----------------------------------------------------------------------
// | RXThinkCMF框架 [ RXThinkCMF ]
// +----------------------------------------------------------------------
// | 版权所有 2017~2020 南京RXThinkCMF研发中心
// +----------------------------------------------------------------------
// | 官方网站: http://www.rxthink.cn
// +----------------------------------------------------------------------
// | Author: 牧羊人 <1175401194@qq.com>
// +----------------------------------------------------------------------

namespace app\admin\widget;


use think\template\TagLib;

/**
 * 站点标签组件
 * @author 牧羊人
 * @since 2020/7/2
 * Class Item
 * @package app\admin\taglib
 */
class Itemcate extends TagLib
{
    // 自定义标签组件
    protected $tags = [
        'select' => ['attr' => "itemId,cateId,limit", 'close' => 0],
    ];

    /**
     * 获取站点栏目
     * @param $tag
     * @return string
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * @since 2021/1/24
     * @author 牧羊人
     */
    public function tagSelect($tag)
    {
        // 标签参数
        $param = $tag['param'];
        // 选中的值
        $value = $tag['value'];

        // 拆分标签名
        $item = explode('|', $param);
        $name = strpos(trim($item[0]), "$") !== false ? ("{" . trim($item[0]) . "}") : trim($item[0]);
        $isRequire = $item[1];
        $showTips = $item[2];
        $isItem = isset($item[3]) ? $item[3] : 0;

        // 获取栏目列表
        $result = $this->getCateList(0);
        $cateList = array();
        if (!empty($result)) {
            foreach ($result as $val) {
                $cateList[] = [
                    'id' => $isItem ? ($val['item_id'] . "," . $val['id']) : $val['id'],
                    'name' => $val["item_name"] . "=>" . $val['name'],
                ];
                if (!empty($val['children'])) {
                    foreach ($val['children'] as $vt) {
                        $cateList[] = [
                            'id' => $isItem ? ($vt['item_id'] . "," . $vt['id']) : $vt['id'],
                            'name' => "|--" . $vt["item_name"] . "=>" . $vt['name'],
                        ];
                        if (!empty($vt['children'])) {
                            foreach ($vt['children'] as $vo) {
                                $cateList[] = [
                                    'id' => $isItem ? ($vo['item_id'] . "," . $vo['id']) : $vo['id'],
                                    'name' => "|--|--" . $vo["item_name"] . "=>" . $vo['name'],
                                ];
                            }
                        }
                    }
                }
            }
        }

        $parse = "<select name=\"{$name}\" id=\"{$name}\" {if $isRequire==1}lay-verify=\"required\"{/if} lay-search=\"\" lay-filter=\"{$name}\">";
        $parse .= "<option value=\"\">【请选择{$showTips}】</option>";
        // 直接传入数据源
        foreach ($cateList as $key => $val) {
            $parse .= '<option value="' . $val["id"] . '" <?php echo (' . $value . ')==' . $val["id"] . ' ? "selected=\"\"" : null ?> >' . $val["name"] . '</option>';
        }
        $parse .= "</select>";
        return $parse;
    }

    /**
     * 获取栏目列表
     * @param int $pid 上级ID
     * @return mixed
     * @since 2021/1/24
     * @author 牧羊人
     */
    private function getCateList($pid = 0)
    {
        $cateModel = new \app\admin\model\ItemCate();
        $cateList = $cateModel->alias('c')
            ->join(DB_PREFIX . 'item i', 'c.item_id=i.id')
            ->where('c.pid', '=', $pid)
            ->where('c.status', '=', 1)
            ->where('i.status', '=', 1)
            ->where('c.mark', '=', 1)
            ->where('i.mark', '=', 1)
            ->order('i.sort ASC,c.sort ASC')
            ->field('c.id,c.name,i.id as item_id,i.name as item_name')
            ->select()->toArray();
        if (!empty($cateList)) {
            foreach ($cateList as &$val) {
                $childList = $this->getCateList($val['id']);
                if (!empty($childList)) {
                    $val['children'] = $childList;
                }
            }
        }
        return $cateList;
    }

}